// miniprogram/pages/clothes_shopping_car/clothes_shopping_car.js
const db=wx.cloud.database()
const _ =db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    box:[],
    money:0,
    box_now:[],
    box_id:[],
    delet_id:[]
  },
  // 计算总金额
  get_money_sum(){
    let that=this
    let money_sum = 0
    for(var x=0;x<that.data.box.length;x++){
      if(that.data.box[x].box_checked == "true"){
        money_sum=money_sum+(that.data.box[x].paper_box_num*that.data.box[x].paper_box_price)
      }
    }
    that.setData({
      money:money_sum
    })

  },

  // 选择事件
  select:function(e){
    let that = this
    console.log(e)
    that.setData({
      delet_id:that.data.delet_id.concat(e.detail.value[0])
    })
    if(e.detail.value.length!==0){
      db.collection('clothes_shopping_car').doc(e.target.dataset.id).update({
        data:{
          box_checked:"true"
        },success:function(res){
          that.onLoad()
        }
      })
    }else{
      db.collection('clothes_shopping_car').doc(e.target.dataset.id).update({
        data:{
          box_checked:""
        },success:function(){
          that.onLoad()
        }
      })
    }
  },

  // 商品删除
  delete:function(){
    let that=this
    wx.cloud.callFunction({
       name:"clothes_delet",
       success:function(res){
         console.log('删除商品成功',res)
         that.onLoad()
       },fail:function(res){
         console.log('删除商品失败',res)
       }
    })

  },

  // 商品数量加事件
  box_jia:function(e){
    let that=this
    console.log(e)
    db.collection('clothes_shopping_car').doc(e.target.dataset.id).update({
      data:{
        paper_box_num:_.inc(1)
      },success:function(res){
        console.log('获取购物车上商品成功',res)
        that.onLoad()
      },fail:function(res){
        console.log('获取购物车价格失败',res)
      }
    })

  },

  // 商品数量减事件
    box_jian:function(e){
    let that=this
    console.log(e)
    if(e.target.dataset.paper_box_num<=1){
      wx.showToast({
        title: '已经不能再少了',
        image:'../../images/title.png'
      })
    }else{
      db.collection('clothes_shopping_car').doc(e.target.dataset.id).update({
        data:{
          paper_box_num:_.inc(-1)
        },success:function(res){
          console.log('获取购物车上商品成功',res)
          that.onLoad()
        },fail:function(res){
          console.log('获取购物车价格失败',res)
        }
      })
  
     
    }
  },
    
  //  计算积分
    get_integral_sum(){
      var integral=0
      let that=this
      let integral_sum=0
      for(var x=0;x<that.data.Recycle.length;x++){
        integral_sum=integral_sum+that.data.Recycle[x].integral
      }
      that.setData({
        integral:integral_sum
      })
      
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    db.collection('Recycle').get().then(res=>{
      that.setData({
        Recycle: res.data
      })
      that.get_integral_sum()
      console.log('Recycle获取成功')
    })

    db.collection('clothes_shopping_car').get({
      success:function(res){
        console.log('获取购物车商品成功',res)
        that.setData({
          box:res.data,
        })
        that.get_money_sum()
      },fail:function(res){
        console.log('获取购物车商品失败',res)
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that=this
    db.collection('clothes_shopping_car').get({
      success:function(res){
        console.log('获取购物车商品成功',res)
        that.setData({
          box:res.data,
        })
        that.get_money_sum()
      },fail:function(res){
        console.log('获取购物车商品失败',res)
      }
    })

  },
  estimate:function(){
    let that=this
    if(that.data.integral>=that.data.money){
      wx.navigateTo({
      url: '../clothes_finalestimate/clothes_finalestimate',
    })
    }else{
      wx.showToast({
        image:"../../images/title.png",
        title: '亲您的积分不足',
      })
    }
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})